from nodetypes.ntypes.Container.showContains.MML import Document, Transform


class Document(Document):    
    
    def filterContainedNodes(self, context, nodes, model, locale):                     
        model = self.getModelByMode(context, model)
                
        action = self.containsAction
        acls = context.acls.checkNodeAcls
        n_extend = nodes.extend
        
        # MAX DISPLAY LIMIT
        # if len(nodes) == max_display_limit:
        #   return
        # Should this be a user preference???
        
        # filter direct children nodes
        def init(n, repo=context.repo):
            try:
                r = repo.fetchResourceByUri(n.rdf_value)
            except:
                return n
            
            n.rdf_reset(
                n.rdf_initDriver(r.getModel().driver)
            )
            n.rdf_domain = r.getAbsolutePathAsUri()
            return n
            
        n_extend(
            n for n in model.mPrimaryTopic \
            if acls(init(n).rdf_getDomain(),action)
        )
                
        # if model IS A translation of another model,
        # then filter the other models matching translated nodes
        # of the children nodes 
        for t in model.mTranslations:
            for c in getattr(t, key):
                n_extend(
                    n for n in c.mTranslation \
                    if locale in n.dcLanguage and \
                        acls(n.rdf_domain, action)
                )

        return nodes


class Transform(Transform):
    
    newDocument = Document